<?php

namespace Admin\Controller;

//导航设置
class NavController extends BaseController {
    
    //导航列表
      public function lst(){

          $Nav_cat =M('Nav_cat');
          $Nav = D('Nav');
          
          $map['status']=1;
          $list = $Nav_cat->field('name,id,pid')->where($map)->select();
          $nav_list = $Nav->list_level($list);
          $this->assign('nav_cat',$nav_list);
          

          $count = $Nav->count();
          $Page = new \Think\Page($count,20);
          $show = $Page->show();
          
          $type=I('type');
          if ($type!=0){
              $map['classify']=$type;
          }
          if (I('search')==''){
              $order ='createtime desc';
          }else {
              $search =trim(I('search'));
              $map['name'] = array('like',"%$search%");
              $order='classify';
          }
          $nav = $Nav->where($map)->order($order)->limit($Page->firstRow.','.$Page->listRows)->select();
          $this->assign('nav',$nav);
          $this->assign('page',$show);
          $this->display();
      }  
    //导航添加
      public function ad(){

          $Nav_cat =M('Nav_cat');
          $Nav = D('Nav');
          $map['status']=1;
          $list = $Nav_cat->where($map)->select();
          $nav_list= $Nav->list_level($list);

          $this->assign('nav_cat',$nav_list);
          

          if (IS_POST){
              $data = array(
                  'name' => I('name'),
                  'tags' => I('tags'),
                  'status' => I('status'),
                  'classify' => I('classify'),
                  'createtime' => time(),
              );

              if ($Nav->create($data)){
                  if ($Nav->add()){
                      $this->success('新增成功',U('lst'));
                  }else {
                      $this->error('新增失败！');
                  }
              }else {
                  $this->error($Nav->getError());
              }
              return ;
          }
          $this->display();
      }  
    //导航修改
      public function edt($id){

          $Nav_cat =M('Nav_cat');
          $Nav = D('Nav');
          $map['status']=1;
          $list = $Nav_cat->field('name,id')->where($map)->select();
          $nav_list= $Nav->list_level($list);
          $this->assign('nav_cat',$nav_list);
          

          $nav=$Nav->find($id);
          $this->assign('nav',$nav);
          if (IS_POST){
              $data =array(
                  'id'=>I('id'),
                  'name'=>I('name'),
                  'tags'=>I('tags'),
                  'status'=>I('status'),
                  'classify'=>I('classify'),
                  'createtime'=>time(),
              );
              if ($Nav->create($data)){
                  if ($Nav->save()){
                      $this->success('修改成功',U('lst'));
                  }else {
                      $this->error('修改失败！');
                  }
              }else {
                  $this->error($Nav->getError());
              }
          
              return ;
          }

          $this->display();
          
      } 
    //导航删除
      public function del($id){
          $Nav =M('Nav');
          if ($Nav->delete($id)){
              $this->success('删除成功！',U('lst'));
          }else {
              $this->error('删除失败');
          }
          
      }
    //导航分类列表
      public function cat_lst(){
          $Nav_cat = M('Nav_cat');
          $Nav = D('Nav');
          $count = $Nav_cat->count();
          $Page = new \Think\Page($count,20);
          $show = $Page->show();
          if (I('search')!==''){
              $search =trim(I('search'));
              $map['name']=array('like',"%$search%");
          }
          $map['status']=1;
          $nav = $Nav_cat->where($map)->order('createtime desc')->limit($Page->firstRow.','.$Page->listRows)->select();
          $nav_list = $Nav->list_level($nav);
          $this->assign('list',$nav_list);
          $this->assign('page',$show);
          $this->display();
          
      }
    //导航分类添加
    public function cat_ad(){
        $Nav_cat = M('Nav_cat');  
        $Nav = D('Nav');
        $nav =$Nav_cat->select();
        $nav_list =$Nav-> list_level($nav);
        $this->assign('nav_list',$nav_list);
        
        if (IS_POST){
            $data = array(
                'name' => I('name'),
                'tags' => I('tags'),
                'status' => I('status'),
                'pid'=>I('pid'),
                'createtime' => time(),
            );
            if ($Nav_cat->create($data)){
                if ($Nav_cat->add()){
                    $this->success('新增成功',U('cat_lst'));
                }else {
                    $this->error('新增失败！');
                }
            }else {
                $this->error($Nav_cat->getError());
            }
            return ;
        }

        $this->display();
    }
   //导航分类修改
      public function cat_edt($id){
          //获取单个分类内容
          $Nav_cat = M('Nav_cat');
          $Nav = D('Nav');
          $nav=$Nav_cat->find($id);
          //获取全部分类
          $list =$Nav_cat->select();
          $nav_list =$Nav->list_level($list);
          $this->assign('list',$nav_list);
          $this->assign('nav',$nav);
          if (IS_POST){
              $data =array(
                  'id'=>I('id'),
                  'name'=>I('name'),
                  'tags'=>I('tags'),
                  'status'=>I('status'),
                  'pid'=>I('pid'),
                  'createtime'=>time(),
              );
              if ($Nav_cat->create($data)){
                  if ($Nav_cat->save()){
                      $this->success('修改成功',U('cat_lst'));
                  }else {
                      $this->error('修改失败！');
                  }
              }else {
                  $this->error($Nav_cat->getError());
              }
          
              return ;
          }
          
          
          $this->display();
          
      }
  //导航分类删除
      public function cat_del($id){
          $Nav_cat =D('Nav_cat');
          if ($Nav_cat->delete($id)){
              $this->success('删除成功！',U('cat_lst'));
          }else {
              $this->error('删除失败');
          }
      }  
    
}